package com.bjdd.jdy.ui.bindCard;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.bjdd.jdy.R;
import com.bjdd.jdy.base.BaseActivity;
import com.bjdd.jdy.entity.BankBean;
import com.bjdd.jdy.util.JumpUtil;
import com.bjdd.jdy.util.StatusBarUtil;
import com.bjdd.jdy.util.T;
import com.zhy.autolayout.AutoLinearLayout;
import com.zhy.autolayout.AutoRelativeLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class BindCardActivity extends BaseActivity<BindCardPresenter, BindCardModel> implements BindCardContract.View {

    @Bind(R.id.toolbar_back)
    ImageView toolbarBack;
    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.tv_toolbar_right)
    TextView tvToolbarRight;
    @Bind(R.id.tv_bank_name)
    TextView tvBankName;
    @Bind(R.id.et_person_name)
    EditText etPersonName;
    @Bind(R.id.et_bank_card_no)
    EditText etBankCardNo;
    @Bind(R.id.all_choose_bank)
    AutoLinearLayout chooseBank;
    private OptionsPickerView pickerView;
    private boolean hasBank = false;
    private List<BankBean> banks;
    private int bankPosition = -1;
    @Override
    public int getLayoutId() {
        return R.layout.activity_bind_card;
    }

    @Override
    public void initView() {
        StatusBarUtil.setColor(this,getResources().getColor(R.color.toolbar_bg),0);
        title.setText("绑定银行卡");
        tvToolbarRight.setText("提交");
        tvToolbarRight.setTextColor(getResources().getColor(R.color.text_black));
        tvToolbarRight.setVisibility(View.VISIBLE);
        pickerView = new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                tvBankName.setText(banks.get(options1).getBankName());
                tvBankName.setTextColor(getResources().getColor(R.color.text_black));
                bankPosition = options1;
            }
        }).build();
    }

    @OnClick({R.id.toolbar_back, R.id.tv_toolbar_right,R.id.all_choose_bank})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.toolbar_back:
                finish();
                break;
            case R.id.tv_toolbar_right:
                if (bankPosition == -1){
                    T.show("请先选择银行");
                    return;
                }
                String realName = etPersonName.getText().toString();
                if (TextUtils.isEmpty(realName) || realName.equals("")){
                    T.show("请先填写姓名");
                    return;
                }
                String cardNo = etBankCardNo.getText().toString();
                if (TextUtils.isEmpty(cardNo) || cardNo.equals("") || cardNo.length() < 15){
                    T.show("请正确输入银行卡卡号");
                    return;
                }
                presenter.bindCard(cardNo,realName,banks.get(bankPosition).getBankId()+"");
                break;
            case R.id.all_choose_bank:
                if (!hasBank){
                    T.show("数据初始化未完成");
                }else {
                    pickerView.show();
                }
                break;
        }
    }

    @Override
    public void BankSuccess(List<BankBean> data) {
        List<String> list = new ArrayList<>();
        banks = data;
        hasBank = true;
        for(int i=0;i<data.size();i++){
            list.add(data.get(i).getBankName());
        }
        pickerView.setPicker(list);
    }

    @Override
    public void bindSuccess() {
        JumpUtil.toBankCard(context);
        finish();
    }
}
